"""
Case Type   : datakit安装
Case Name   : 【服务器管理】用户管理新增、编辑、删除用户
Create At   : 2025/5/20
Owner       : @lonely-dance
Description :
    1.添加服务器
    2.添加用户
    3.编辑添加的用户
    4.删除用户
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.pri_root_node = Node('PrimaryRoot')
        self.pri_node = Node('PrimaryDbUser')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.com_datakit = CommonDatakit(self.token)

    def test_base_ops(self):
        text = f'----step1:添加服务器 expect：成功----'
        self.log.info(text)
        self.com_datakit.import_host([self.pri_root_node])

        text = f'----step2:添加用户 expect:成功----'
        self.log.info(text)
        data = {"privateIp": f"{self.pri_node.ssh_host}", "publicIp": f"{self.pri_node.ssh_host}", "port": 22,
                "password": f"{self.pri_node.ssh_password}",
                "username": f"{self.pri_node.ssh_user}"}
        response = self.com_api.post_host_ping(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)
        response = self.com_api.get_host_page(name=f"{self.pri_root_node.ssh_host}")
        self.log.info(response.json())
        host_id = response.json()['rows'][0]['hostId']
        use_msg = {"hostId": f"{host_id}", "username": f"{self.pri_node.ssh_user}",
                   "password": f"{self.pri_node.ssh_password}"}
        response = self.com_api.post_hostUser(use_msg)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = f'----step3:编辑用户 expect:成功----'
        self.log.info(text)
        response = self.com_api.get_hostUser_page(host_id)
        self.log.info(response.json())
        host_user_id = ''
        for item in response.json()['rows']:
            if item['username'] == self.pri_node.ssh_user:
                host_user_id = item['hostUserId']
                break
        data = {"id": host_user_id, "hostId": host_id, "privateIp": f"{self.pri_node.ssh_host}",
                "publicIp": f"{self.pri_node.ssh_host}", "port": 22, "username": f"{self.pri_node.ssh_user}",
                "password": f"{self.pri_node.ssh_password}"}
        response = self.com_api.put_hostUser(host_user_id=host_user_id, data=data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = '----step4:删除用户 expect:成功'
        self.log.info(text)
        response = self.com_api.delete_hostUser(host_user_id=host_user_id)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step5:清理环境 expect:成功----')
        self.com_datakit.delete_host()
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
